package com.allesblinkt.mucom.client;

import java.io.File;
import java.util.Hashtable;
import java.util.Vector;

import com.allesblinkt.mucom.common.Constants;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PImage;

public class NotifyRepresentation implements Constants {

	private boolean show = true;
	
	private ClientApp app;
	
	private Sprite sprite;
	
	private final Hashtable<NotifyState, Vector<Sprite>> pictures;
	
	private int frameCount = 0;
	private float appeartime = 0;

	
	private Enum<NotifyState> notificationState;

	private boolean isPermanent;


	public NotifyRepresentation(ClientApp theApp) {
		

		app = theApp;
		
		pictures = new Hashtable<NotifyState, Vector<Sprite>>();
		
		notificationState = NotifyState.WELCOME;
		
		
		preload();
		
		setOnce(notificationState, 10);

		
	}
	
	
	public void setPermanent(Enum<NotifyState> theState){
		

		show();
		
		isPermanent = true;
		
		notificationState = theState;
	
		
		Vector<Sprite> theSprites = pictures.get(notificationState);
		
		if(theSprites != null){
			
			Sprite theSprite = theSprites.get((int) PApplet.min(theSprites.size()-1, app.random(0,theSprites.size())));
			
			if(theSprite != null){
			
				sprite = theSprite;

			}

		}

	
	}
	
	
	public void setOnce(Enum<NotifyState> theState, float theTime){
		setPermanent(theState);
		
		frameCount = 0;
		isPermanent = false;
		appeartime = theTime;
	}
	
	
	public void show(){
		show = true;
	}
	
	public void hide(){
		show = false;
	}
	
	public void draw(){
		
		app.pushStyle();
		
		if(show && sprite != null){
			sprite.draw(app);
		}
		
		if(!isPermanent && (float)frameCount/FRAMERATE > appeartime) show = false;
		
		app.popStyle();
		
		frameCount++;
	}
	
	public boolean isShown(){
		return show;
	}
	
	

	
	private void preload(){
		
		/* This is complete bs. If anyone has a more schnafte way to implement this... Do tell */

		Vector<Sprite> welcome = new Vector<Sprite>();
		welcome.add(new Sprite(app.loadImage("client/messages/"+"welcome_1.png")));
		welcome.add(new Sprite(app.loadImage("client/messages/"+"welcome_2.png")));
		pictures.put(NotifyState.WELCOME, welcome);		
		
		Vector<Sprite> angry = new Vector<Sprite>();
		angry.add(new AnimatedSprite(app.loadImage("client/messages/"+"angry_1_a.png"),app.loadImage("client/messages/"+"angry_1_b.png")));
		pictures.put(NotifyState.ANGRY, angry);		
		
		Vector<Sprite> attention = new Vector<Sprite>();
		attention.add(new Sprite(app.loadImage("client/messages/"+"attention_1.png")));
		attention.add(new Sprite(app.loadImage("client/messages/"+"attention_2.png")));
		pictures.put(NotifyState.ATTENTION, attention);		
		
		Vector<Sprite> solitude = new Vector<Sprite>();
		solitude.add(new Sprite(app.loadImage("client/messages/"+"solitude_1.png")));
		solitude.add(new Sprite(app.loadImage("client/messages/"+"solitude_2.png")));
		pictures.put(NotifyState.SOLITUDE, solitude);		
		
		Vector<Sprite> theft = new Vector<Sprite>();
		theft.add(new Sprite(app.loadImage("client/messages/"+"theft_1.png")));
		theft.add(new Sprite(app.loadImage("client/messages/"+"theft_2.png")));
		pictures.put(NotifyState.THEFT, theft);	
		
		Vector<Sprite> upload_allowed = new Vector<Sprite>();
		upload_allowed.add(new Sprite(app.loadImage("client/messages/"+"upload_allowed_1.png")));
		pictures.put(NotifyState.UPLOAD_ALLOWED, upload_allowed);	

		Vector<Sprite> upload_denied = new Vector<Sprite>();
		upload_denied.add(new AnimatedSprite(app.loadImage("client/messages/"+"upload_denied_1_a.png"), app.loadImage("client/messages/"+"upload_denied_1_b.png")));
		pictures.put(NotifyState.UPLOAD_DENIED, upload_denied);	
		
		Vector<Sprite> uploading = new Vector<Sprite>();
		uploading.add(new AnimatedSprite(app.loadImage("client/messages/"+"uploading_1_a.png"),app.loadImage("client/messages/"+"uploading_1_b.png")));
		pictures.put(NotifyState.UPLOADING, uploading);	
		
		Vector<Sprite> downloaded = new Vector<Sprite>();
		downloaded.add(new AnimatedSprite(app.loadImage("client/messages/"+"downloaded_1_a.png"), app.loadImage("client/messages/"+"downloaded_1_b.png")));
		pictures.put(NotifyState.DOWNLOADED, downloaded);
		
	}
	
	
}
